package com.miyu.system.controller.log;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.utils.commonUtils.Rediscache;
import com.miyu.system.service.log.SysLogService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 系统日志控制器
 *
 * @author longm
 * @date 2022/10/03
 */
@RestController
@RequestMapping(value = "/sys-log")
public class SysLogController {

    @Resource
    private SysLogService sysLogService;

    /**
     * api调用日志
     * @param param
     * @return
     */
    @RequestMapping("queryApiLog")
    public JSONArray queryApiLog(@RequestBody JSONObject param){
        //从redis中查找日志记录
        JSONArray apiLog = Rediscache.getJSONArray(1, "log:api-log");
        String st = "beginDate";
        String en ="endDate";
        Date  beginDate;
        Date  endDate;
        Date date = DateUtil.date();
        if(param.containsKey(st)){
            beginDate = DateUtil.beginOfDay(DateUtil.parse(param.getString("beginDate")));
        }else{
            beginDate = DateUtil.beginOfDay(date);
        }
        if(param.containsKey(en)){
            endDate = DateUtil.endOfDay(DateUtil.parse(param.getString("endDate")));
        }else {
            endDate = DateUtil.endOfDay(date);
        }
        DateTime begin = new DateTime(beginDate);
        DateTime end = new DateTime(endDate);
        JSONArray rs = apiLog.stream()
                .filter(item -> DateUtil.parse(((JSONObject)item).getString("operateTime")).isIn(begin,end))
                .collect(Collectors.toCollection(JSONArray::new));
        return  rs;
    }

    /**
     * 查询登录日志
     *
     * @param param 参数
     * @return {@link JSONArray}
     */
    @RequestMapping("queryLoginLog")
    public JSONArray queryLoginLog(@RequestBody JSONObject param){
        //从redis中查找日志记录
        JSONArray apiLog = Rediscache.getJSONArray(1, "log:login-log");
        String st = "beginDate";
        String en ="endDate";
        Date  beginDate;
        Date  endDate;
        Date date = DateUtil.date();
        if(param.containsKey(st)){
            beginDate = DateUtil.beginOfDay(DateUtil.parse(param.getString("beginDate")));
        }else{
            beginDate = DateUtil.beginOfDay(date);
        }
        if(param.containsKey(en)){
            endDate = DateUtil.endOfDay(DateUtil.parse(param.getString("endDate")));
        }else {
            endDate = DateUtil.endOfDay(date);
        }
        DateTime begin = new DateTime(beginDate);
        DateTime end = new DateTime(endDate);
        JSONArray rs = apiLog.stream()
                .filter(item -> DateUtil.parse(((JSONObject)item).getString("operateTime")).isIn(begin,end))
                .collect(Collectors.toCollection(JSONArray::new));
        return  rs;
    }

    /**
     * 查询系统日志设置
     * @return {@link JSONArray}
     */
    @RequestMapping("querySysLogSet")
    public JSONArray querySysLogSet(@RequestBody JSONObject param){
       return sysLogService.querySysLogSet(param);
    }

    /**
     * 拯救系统日志设置
     * @param param 参数
     * @return {@link JSONObject}
     */
    @RequestMapping("saveSysLogSet")
    public JSONObject saveSysLogSet(@RequestBody JSONObject param){
        return sysLogService.saveSysLogSet(param);
    }

    /**
     * 删去系统日志设置
     * @param params 参数
     * @return {@link JSONObject}
     */
    @RequestMapping("delSysLogSet")
    public JSONObject delSysLogSet(@RequestBody JSONArray params){
        return sysLogService.delSysLogSet(params);
    }

    /**
     * 同步系统日志设置
     * @return {@link JSONObject}
     */
    @RequestMapping("syncSysLogSet")
    public JSONObject syncSysLogSet(){
        return  sysLogService.syncSysLogSet();
    }
}
